﻿namespace Baddies.GUI
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Baddies.Events;
    using Baddies.Scenes;  
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;        

    /// <summary>
    /// Class for outputting text.
    /// </summary>
    public class TextOutputGUI : BoxGUI
    {
        /// <summary>
        /// Reference to the SpriteFont we are going to use.
        /// </summary>
        private SpriteFont spriteFont;

        /// <summary>
        /// Text to write.
        /// </summary>
        private List<string> text;

        /// <summary>
        /// Color to apply to the text.
        /// </summary>
        private Color textColor;

        /// <summary>
        /// Maximum number of lines allowed.
        /// </summary>
        private int maxLines;

        /// <summary>
        /// Sets the color of the text.
        /// </summary>
        /// <value>Color we want to apply to the text.</value>
        public Color TextColor
        {
            set { this.textColor = value; }
        }

         /// <summary>
        /// Initializes a new instance of the TextOutputGUI class.
        /// </summary>
        public TextOutputGUI()
        {
            this.spriteFont = Scene.Content.Load<SpriteFont>("Fonts/ConsoleFont");            
            this.text = new List<string>();
            this.textColor = Color.White;

            this.maxLines = 12;

            this.H = this.spriteFont.LineSpacing * this.maxLines;

            EventManager.GetEV.RegisterNewEvent("AddLine", false);
            EventManager.GetEV.Register("AddLine", this.AddLineEventDelegate);
        }

        /// <summary>
        /// Adds a new line to the text output, respecting the maximum number of lines.
        /// If there are too many lines, it pops the first one.
        /// </summary>
        /// <param name="line">Line to write.</param>
        public void AddLine(string line)
        {
            if (this.text.Count >= this.maxLines)
            {
                this.text.RemoveAt(0);
            }

            this.text.Add(line);
        }

        /// <summary>
        /// Addas a text with one or more lines to the text output.
        /// </summary>
        /// <param name="text">Text to add.</param>
        public void AddText(string text)
        {
            string[] lines = text.Split(System.Environment.NewLine.ToCharArray());
            foreach (string line in lines)
            {
                if (line.Length > 0)
                {
                    this.AddLine(line);
                }
            }
        }

        /// <summary>
        /// Delegate function for the AddLine event.
        /// </summary>
        /// <param name="sender">Sender of the request.</param>
        /// <param name="e">AddLineArg parameter.</param>
        private void AddLineEventDelegate(object sender, EventArgs e)
        {
            this.AddText(((AddLineArg)e).Text);
        }

        /// <summary>
        /// Draws the text box and the number of lines specified.
        /// </summary>
        /// <param name="spritebatch">SpriteBatch to draw on.</param>
        public override void Draw(SpriteBatch spritebatch)
        {
            base.Draw(spritebatch);

            float y = this.DrawPosition.Y;
            foreach (string line in this.text)
            {
                spritebatch.DrawString(
                this.spriteFont,
                line,
                new Vector2(this.DrawPosition.X, y),
                this.textColor);

                y += this.spriteFont.LineSpacing;
            }
        }
    }
}
